package logic

import (
	"context"
	"fmt"
	"shopconnect/model/mysql"
	"shopconnect/model/redis"

	"shopconnect/shoprpc/internal/svc"
	"shopconnect/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *shoprpc.UserLoginRequest) (*shoprpc.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Rdb.Get("sendSmslogin" + in.Mobile)
	if get.Val() != in.SendCode {
		return nil, fmt.Errorf("当前验证码不正确")
	}
	var user mysql.User
	err := mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&user).Error
	if err != nil {
		return nil, fmt.Errorf("当前用户不存在")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("密码错误,请重新输入")
	}
	redis.Rdb.Del("sendSmslogin" + in.Mobile)
	return &shoprpc.UserLoginResponse{Id: int64(user.ID)}, nil
}
